﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using TileEngine.tiles;

namespace TileEngine.liquids
{
    class LiquidEngine
    {
        World parentWorld;
        public List<Vector2> dynamicLiquids = new List<Vector2>();
        public LiquidEngine(World parent)
        {
            parentWorld = parent;
        }
        public void update()
        {
            int x;
            int y;


            for (int i=dynamicLiquids.Count-1;i>-1 ; i--)
            {
                x=(int)dynamicLiquids[i].X;
                y=(int)dynamicLiquids[i].Y;
                 if (getLiquid(x, y).getParticle() <= 0.01f)
                {
                    dynamicLiquids.RemoveAt(i);
                    parentWorld.tiles[x,y].liquid = null;
                    
                }else if (checkDown(x, y))
                {
                    if (getLiquid(x, y).getParticle() <= 0.01f)
                    {
                        dynamicLiquids.RemoveAt(i);
                        parentWorld.tiles[x,y].liquid = null;
                    }
                }
                else
                {
                    checkLeft(x, y);
                    checkRight(x, y);
                    if (getLiquid(x, y).getParticle() <= 0f)
                    {
                        dynamicLiquids.RemoveAt(i);
                        parentWorld.tiles[x,y].liquid = null;
                    }
                }
            }
        }

        bool checkDown(int x,int y)
        {
            if (!isEmpty(x,y+1))
                return false;
            if (!isLiquid(x, y + 1))
                createLiquid(x, y + 1);
            if (getLiquid(x, y + 1).getParticle() >= 10)
                return false;

            getLiquid(x, y + 1).addParticle(getLiquid(x,y).getParticle());
            getLiquid(x, y).setParticle(0);
            //getLiquid(x, y).addParticle(-2);
            return true;
        }
        bool checkRight(int x,int y)
        {
            if (getLiquid(x, y).getParticle() < 1f)
                return false;
            if (!isEmpty(x+1, y))
                return false;
            if (!isLiquid(x + 1, y))
                createLiquid(x + 1, y);
            if (getLiquid(x + 1, y).getParticle() > getLiquid(x, y).getParticle())
                return false;
            
            float dif = (getLiquid(x, y).getParticle() - getLiquid(x + 1, y).getParticle()) / 2;
            if (dif < 0)
                return false;
            getLiquid(x, y).addParticle(-dif);
            getLiquid(x+1, y ).addParticle(dif);
            return true;
        }
        bool checkLeft(int x,int y)
        {
            if (getLiquid(x, y).getParticle() < 1f)
                return false;
            if (!isEmpty(x-1, y))
                return false;
            if (!isLiquid(x - 1, y))
                createLiquid(x - 1, y);
            if (getLiquid(x - 1, y).getParticle() > getLiquid(x, y).getParticle())
                return false;

           
            float dif = (getLiquid(x, y).getParticle() - getLiquid(x - 1, y).getParticle()) / 2;
            if (dif < 0)
                return false;
            getLiquid(x, y).addParticle(-dif);
            getLiquid(x-1, y).addParticle(dif);
            return true;
        }
        Iliquid getLiquid(int x, int y) {
            return parentWorld.tiles[x,y].liquid;
        }
        bool isEmpty(int x, int y)
        {
            if (parentWorld.tiles[x,y].tile.GetType() == typeof(Empty))
                return true;
            return false;
        }
        bool isLiquid(int x,int y)
        {
            if (parentWorld.tiles[x,y].liquid != null)
                return true;
            return false;
        }
        void createLiquid(int x, int y)
        {
            Water newWater = new Water();
            newWater.setParticle(0);
            parentWorld.tiles[x,y].liquid = newWater;


            dynamicLiquids.Add(new Vector2(x, y));
        }
    }
}
